<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers\Example;

use App\Filters\Example\ExampleTableFilter;
use App\Http\Controllers\Controller;
use App\Http\Requests\Example\ExampleTableRequest;
use App\Http\Resources\Example\ExampleTableCollection;
use App\Http\Resources\Example\ExampleTableResource;
use App\Models\Example\ExampleTable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 普通表格演示模块
 * Class ExampleTableController
 * @package App\Http\Controllers
 */
class ExampleTableController extends Controller
{
    /**
     * 列表
     * @param ExampleTableFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(ExampleTableFilter $filter)
    {
        $filter = ExampleTable::filter($filter)->paginate($this->pageSize());
        $collection = ExampleTableCollection::make($filter)->only(['id','title','cover_image','recommend_type','created_at','updated_at','status'])->toArray(null);
        return $this->success('', $collection);
    }

    /**
     * 添加
     * @param ExampleTableRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(ExampleTableRequest $request)
    {
        $data = $request->validate('store');
        $example = new ExampleTable($data);
        if (!$example->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param ExampleTable $example_table
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ExampleTable $example_table)
    {
        $exampleTableResource = ExampleTableResource::make($example_table)->except(['updated_at'])->toArray(null);
        return $this->success('', $exampleTableResource);
    }

    /**
     * 更新
     * @param ExampleTableRequest $request
     * @param ExampleTable $example_table
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(ExampleTableRequest $request, ExampleTable $example_table)
    {
        $data = $request->validate('update');
        if (!$example_table->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param $id // 1或'1,2'
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            ExampleTable::destroy($between);
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param ExampleTableRequest $request
     * @param ExampleTable $example_table
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(ExampleTableRequest $request, ExampleTable $example_table)
    {
        $data = $request->validate('updateStatus');
        if (!$example_table->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 更新排序
     * @param ExampleTableRequest $request
     * @param ExampleTable $example_table
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(ExampleTableRequest $request, ExampleTable $example_table)
    {
        $data = $request->validate('updateSort');
        if (!$example_table->batchUpdate($data['sort'])) return $this->errorUpdated();
        return $this->successUpdated();
    }
}
